new Image().src = "/img/common/loading.gif"; //预加载

function ShowMsg(data) {
  layer.open({
    type: 1
    , title: false
    , area: ['320px']
    , content: '<div style="font-size: 2em;background-color: #393D49;padding:15px;line-height:1.5em;"><pre>' + data + "</pre></div>"
    , btm: ['了解']
    , closeBtn: true
  });
}
function FindPwd(data) {
  ShowMsg("网络连接失败, 请检查网线是不是被挖掘机挖掉了.");
  return false;
}

function RENDERCLOCK() {
  var clock = new Vue({
  	el: '#clock'
    , data: {
      time: ''
      , date: ''
    }
  });
  var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  setInterval(updateTime, 1000, clock, week);
  updateTime(clock, week);
}
function updateTime(clock, week) {
  var cd = new Date();
  clock.time = zeroPadding(cd.getHours(), 2) + ':' + zeroPadding(cd.getMinutes(), 2) + ':' + zeroPadding(cd.getSeconds(), 2);
  clock.date = zeroPadding(cd.getFullYear(), 4) + '-' + zeroPadding(cd.getMonth()+1, 2) + '-' + zeroPadding(cd.getDate(), 2) + ' ' + week[cd.getDay()];
};
function zeroPadding(num, digit) {
  var zero = '';
  for(var i = 0; i < digit; i++) {zero += '0';}
  return (zero + num).slice(-digit);
}

function Viewer(obj) {
	let bro = obj.previousElementSibling;
	let textContent = bro.attributes._textContent.value.trim();
  layer.open({
    type: 1
    , title: '<center><h2>' + lang[textContent] + '</h2></center>' //不显示标题栏
    , closeBtn: false
    , area: '800px'
    , shade: 0.8
    , shadeClose : true
    , id: 'LAY_layuipro' //设定一个id，防止重复弹出
    , btn: ['了解']
    , btnAlign: 'c'
    , moveType: 1 //拖拽模式，0或者1
    , content: '<div style="font-size: 2em;background-color: #393D49;padding:15px;line-height:1.5em;">' + lang[textContent + 'miaoshu'] + "</div>"
    , yes: function(layero){
    	layer.close(layero);
    }
  });
}

function init() {
  window.$sc = { //selector_cache
    divLoading: $("#loading"),
    divLoadingImg: $("#loading_img"),
    divMain: $("#main"),
    gBody: $("body"),
    divContent: $("#mainView"),
  };
  window.$nc = { //necessity_cache
    gHash: window.location.hash,
    w: $(window).width(),
    h: $(window).height(), //获取窗体尺寸
  }; //全局变量
  if (!$nc.gHash) {
    window.location.hash = "#m";
  }
  titleChanger(); //修改<title>
  divSelfAdaptive_1(); //自适应
  bodySelectBg(); //背景图
}

function divSelfAdaptive_1() { //窗体自适应函数
  if ($sc == undefined || $nc == undefined) {
    init();
  }
  $sc.divLoadingImg.css("margin-top", ($nc.h / 2) - 31); //"加载中"自适应
  $sc.divMain.css("height", ($nc.h - 70));
};

function bodySelectBg() { //背景更换
  if ($sc == undefined || $nc == undefined) {
    init();
  }
  switch ($nc.gHash) {
    default: $sc.gBody.css("background-image", "url(/img/bgs/LuoYang_1_1000.jpg)");
    break;
  }
};

function titleChanger() { //修改<title>
  if ($sc == undefined || $nc == undefined) {
    init();
  }
  switch ($nc.gHash) {
    case "#m":
      document.title = '首页 - 我的手艺';
      break;
    case "#level":
      document.title = '等级制度 - 我的手艺';
      break;
    case "#show":
      document.title = '作品展示 - 我的手艺';
      break;
    case "#joinus":
    	document.title = '加入我们 - 我的手艺';
    	break;
    default:
    	document.title = '世界, 你我共建 - 我的手艺';
    	break;
  }
};

function loadContent() { //加载主要内容
  if ($sc == undefined || $nc == undefined) {
    init();
  }
  $sc.divLoading.fadeIn(600);
  switch ($nc.gHash) {
    case "#m":
      $sc.divContent.hide(500, function() {
        $sc.divContent.empty().prepend(mainPageContent.innerHTML).show(500);
      });
      break;
    case "#level":
      $sc.divContent.hide(500, function() {
        $sc.divContent.empty().prepend(mainLevel.innerHTML).show(500);
      });
      break;
    case "#show":
      $sc.divContent.hide(500, function() {
        $sc.divContent.empty().prepend(mainShow.innerHTML).show(500);
      });
      break;
    case "#joinus":
      $sc.divContent.hide(500, function() {
        $sc.divContent.empty().prepend(mainJoin.innerHTML).show(500);
      });
      break;
    default:
      $sc.divContent.hide(500, function() {
        $sc.divContent.empty().prepend(l404Content.innerHTML).show(500);
        RENDERCLOCK();
        var ffffflag = 0;
        Init404();
      });
      break;
  }
	$sc.divLoading.fadeOut(118);
};

$(document).ready(function() {
  init();
});

$(window).resize(function() {
  $nc.h = $(window).height();
  $nc.w = $(window).width();
  divSelfAdaptive_1(); //自适应
});
$(window).load(function() {
  loadContent(); //载入主内容
});
$(window).bind("hashchange", function(e) {
  $nc.gHash = window.location.hash;
  titleChanger(); //修改<title>
  bodySelectBg(); //背景图
  loadContent(); //载入主内容
});